package com.odd.webapp.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;

import com.google.gwt.user.client.Window;
import java.util.Iterator;
import com.odd.webapp.businessobjects.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Open_data_dictionary implements EntryPoint {
  /**
   * The message displayed to the user when the server cannot be reached or
   * returns an error.
   */
  private static final String        SERVER_ERROR    = "An error occurred while "
                                                         + "attempting to contact the server. Please check your network "
                                                         + "connection and try again.";

  /**
   * Create a remote service proxy to talk to the server-side Greeting service.
   */
  private static final Open_data_dictionaryServiceAsync service = GWT.create(Open_data_dictionaryService.class);
  
  interface Binder extends UiBinder<DockLayoutPanel, Open_data_dictionary> { }
  
  private static final Binder binder = GWT.create(Binder.class);


  @UiField VerticalPanel topPanel;
  @UiField VerticalPanel westNorthPanel;
  @UiField VerticalPanel westSouthPanel;
  @UiField VerticalPanel northPanel;
  @UiField VerticalPanel mainPanel;
  
  HashMap<Anchor,Category>  categoryMap = new HashMap<Anchor,Category>();
  

  public static Open_data_dictionaryServiceAsync getService(){
    return service;
  }
  
  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    
    GWT.log("Hello World!", null);
    
    /*SplitLayoutPanel p = new SplitLayoutPanel();
    p.addWest(new HTML("navigations"), 300);
    p.addNorth(new HTML("lists"), 384);
    p.add(new HTML("details"));
    
    RootPanel.get().add(p);*/

    DockLayoutPanel outer = binder.createAndBindUi(this);
    
    // Add the outer panel to the RootLayoutPanel, so that it will be
    // displayed.
    //RootLayoutPanel root = RootLayoutPanel.get();
    
    //
    topPanel.add(new HTML("<a href='#test'>top</a>"));
    westNorthPanel.add(new HTML("<a href='#test'>west1</a>"));
    westSouthPanel.add(new HTML("<a href='#test'>west2</a>"));
    northPanel.add(new HTML("<a href='#test'>north</a>"));
    mainPanel.add(new HTML("<a href='#test'>main</a>"));
    
    
    
    
    RootLayoutPanel root = RootLayoutPanel.get();
    //root.get("ODDContent").add(outer); 
    //RootPanel.get("ODDContent").insert(outer, 0);
    root.add(outer);
    
    loadCategorys("");
    
    
    class CategoryCreate implements ClickHandler {
      /**
       * Fired when the user clicks on the sendButton.
       */
      public void onClick(ClickEvent event) {
        mainPanel.remove(0);
        CategoryEditPanel fp = new CategoryEditPanel();
        mainPanel.add(fp);
      }
    }
    
    class FieldCreate implements ClickHandler {
      /**
       * Fired when the user clicks on the sendButton.
       */
      public void onClick(ClickEvent event) {
        mainPanel.remove(0);
        FieldEditPanel fp = new FieldEditPanel("");
        mainPanel.add(fp);
      }
    }
    
    
    final Button sendButton4 = new Button("Create Category");
    westNorthPanel.add(sendButton4);
    CategoryCreate handler4 = new CategoryCreate();
    sendButton4.addClickHandler(handler4);
    
    final Button sendButton3 = new Button("Create Field");
    westSouthPanel.add(sendButton3);
    FieldCreate handler3 = new FieldCreate();
    sendButton3.addClickHandler(handler3);
    
  }
  
  
  private void loadCategorys(String parentName){
    service.getCategorys(parentName, new AsyncCallback<Category[]>() {
      public void onFailure(Throwable caught) {
        GWT.log("Server failure!", null);
      }

      public void onSuccess(Category[] result) {
        GWT.log("Server result!", null);
        for(int i=0;i<result.length;i++){
          
          Anchor f1 = new Anchor(result[i].getName());//HTML("<a>"+result[i].getName()+"</a>");
          
          categoryMap.put(f1, result[i]);
          westNorthPanel.add(f1);
          
          f1.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
              Object sender = event.getSource();
              Category c = categoryMap.get(sender);
              GWT.log("Clicked "+c.getFullName(), null);
              loadFields(c.getFullName());
            }
           });
        }
      }
    });
  }
  
  private void loadFields(String categoryName){
    service.getFields(categoryName, new AsyncCallback<Field[]>() {
      public void onFailure(Throwable caught) {
        GWT.log("Server failure!", null);
      }

      public void onSuccess(Field[] result) {
        GWT.log("Server result!", null);
        for(int i=0;i<result.length;i++){
          
          Anchor f1 = new Anchor(result[i].getName());//HTML("<a>"+result[i].getName()+"</a>");
          
          westSouthPanel.add(f1);
          
          f1.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
              GWT.log("Clicked", null);
            }
           });
        }
      }
    });
  }
  
  
}

/*FieldCreateDialogBox db = new FieldCreateDialogBox("test1");
db.dialogBox.setGlassEnabled(true);
db.dialogBox.setAnimationEnabled(true);
db.dialogBox.center();
db.dialogBox.show();*/
